package vip.specimen.system.datasource.validation.question;

import vip.specimen.system.datasource.entity.question.QuestionColumnEntity;
import vip.specimen.system.service.question.ColumnService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/4/26 16:41
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
public class SuperiorNotSelfImp implements ConstraintValidator<SuperiorNotSelf, UUID> {
    @Resource
    private ColumnService columnService;
    @Resource
    private HttpServletRequest request;


    @Override
    public boolean isValid(UUID uuid, ConstraintValidatorContext constraintValidatorContext) {
        if (null == uuid) return true;
        QuestionColumnEntity columnEntity = this.columnService.findById(uuid);
        if (columnEntity.getUuid().toString().equals(request.getParameter("uuid"))) return false;
        return valid(columnEntity);//递归查询 是否是子级
    }


    private boolean valid(QuestionColumnEntity columnEntity) {
        if (null == columnEntity.getSuperior()) return true;
        else if (columnEntity.getSuperior().getUuid().toString().equals(request.getParameter("uuid"))) return false;
        return valid(columnEntity.getSuperior());
    }
}
